package com.atguigu.day06;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2024/4/7
 * 该案例演示了水位线传递
 */
//TODO 指定流处理环境
//TODO 指定并行度
//TODO 从指定的网络端口中读取数据
//TODO 指定Watermark的生成策略并提取事件时间字段
//TODO 对流中读取的数据进行类型转换   String->WaterSensor
//TODO 按照传感器id进行分组
//TODO 开窗
//TODO 对窗口中的数据进行处理
//TODO 打印结果
//TODO 提交作业
public class Flink08_Watermark_Pass_1 {
    public static void main(String[] args) throws Exception {
        //TODO 指定流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //TODO 指定并行度
        env.setParallelism(2);
        //TODO 从指定的网络端口中读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 指定Watermark的生成策略并提取事件时间字段
        SingleOutputStreamOperator<String> withWatermarkDS = socketDS.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<String>forMonotonousTimestamps()
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<String>() {
                                    @Override
                                    public long extractTimestamp(String lineStr, long recordTimestamp) {
                                        String[] fieldArr = lineStr.split(",");
                                        return Long.valueOf(fieldArr[1]);
                                    }
                                }
                        )
        );
        //TODO 对流中读取的数据进行类型转换   String->WaterSensor
        SingleOutputStreamOperator<WaterSensor> wsDS = withWatermarkDS.map(new WaterSensorMapFunction());
        //TODO 按照传感器id进行分组
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);
        //TODO 开窗
        WindowedStream<WaterSensor, String, TimeWindow> windowDS = keyedDS.window(TumblingEventTimeWindows.of(Time.milliseconds(10)));
        //TODO 对窗口中的数据进行处理
        SingleOutputStreamOperator<String> processDS = windowDS.process(
                new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    @Override
                    public void process(String s, ProcessWindowFunction<WaterSensor, String, String, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        long count = elements.spliterator().estimateSize();
                        String windowStart = DateFormatUtils.format(context.window().getStart(), "yyyy-MM-dd HH:mm:ss.SSS");
                        String windowEnd = DateFormatUtils.format(context.window().getEnd(), "yyyy-MM-dd HH:mm:ss.SSS");
                        out.collect("key=" + s + "的窗口[" + windowStart + "," + windowEnd + ")包含" + count + "条数据===>" + elements.toString());
                    }
                }
        );
        //TODO 打印结果
        processDS.print();
        //TODO 提交作业
        env.execute();
    }
}
